package xBei.Helper
{
	import xBei.Drawing.CMYKColor;
	import xBei.Drawing.RGBColor;

	public final class ColorHelper
	{
		public static const blue:uint = 0xFF;
		public static const green:uint = 0x8000;
		public static const red:uint = 0xFF0000;
		public static const aliceblue:uint = 15792383;
		public static const antiquewhite:uint = 16444375;
		public static const aqua:uint = 0xFFFF;
		public static const aquamarine:uint = 8388564;
		public static const azure:uint = 15794175;
		public static const beige:uint = 16119260;
		public static const bisque:uint = 16770244;
		public static const black:uint = 0;
		public static const blanchedalmond:uint = 16772045;
		public static const blueviolet:uint = 9055202;
		public static const brown:uint = 10824234;
		public static const burlywood:uint = 14596231;
		public static const cadetblue:uint = 6266528;
		public static const chartreuse:uint = 0x7FFF00;
		public static const chocolate:uint = 13789470;
		public static const coral:uint = 16744272;
		public static const cornflowerblue:uint = 6591981;
		public static const cornsilk:uint = 16775388;
		public static const crimson:uint = 14423100;
		public static const cyan:uint = 0xFFFF;
		public static const darkblue:uint = 139;
		public static const darkcyan:uint = 0x8B8B;
		public static const darkgoldenrod:uint = 12092939;
		public static const darkgray:uint = 0xA9A9A9;
		public static const darkgreen:uint = 0x6400;
		public static const darkgrey:uint = 0xA9A9A9;
		public static const darkkhaki:uint = 12433259;
		public static const darkmagenta:uint = 0x8B008B;
		public static const darkolivegreen:uint = 5597999;
		public static const darkorange:uint = 0xFF8C00;
		public static const darkorchid:uint = 10040012;
		public static const darkred:uint = 0x8B0000;
		public static const darksalmon:uint = 15308410;
		public static const darkseagreen:uint = 9419919;
		public static const darkslateblue:uint = 4734347;
		public static const darkslategray:uint = 3100495;
		public static const darkslategrey:uint = 3100495;
		public static const darkturquoise:uint = 52945;
		public static const darkviolet:uint = 0x9400D3;
		public static const deeppink:uint = 16716947;
		public static const deepskyblue:uint = 49151;
		public static const dimgray:uint = 0x696969;
		public static const dimgrey:uint = 0x696969;
		public static const dodgerblue:uint = 2003199;
		public static const firebrick:uint = 11674146;
		public static const floralwhite:uint = 16775920;
		public static const forestgreen:uint = 2263842;
		public static const fuchsia:uint = 0xFF00FF;
		public static const gainsboro:uint = 0xDCDCDC;
		public static const ghostwhite:uint = 16316671;
		public static const gold:uint = 0xFFD700;
		public static const goldenrod:uint = 14329120;
		public static const gray:uint = 0x808080;
		public static const grey:uint = 0x808080;
		public static const greenyellow:uint = 11403055;
		public static const honeydew:uint = 15794160;
		public static const hotpink:uint = 16738740;
		public static const indianred:uint = 13458524;
		public static const indigo:uint = 0x4B0082;
		public static const ivory:uint = 16777200;
		public static const khaki:uint = 15787660;
		public static const lavender:uint = 15132410;
		public static const lavenderblush:uint = 16773365;
		public static const lawngreen:uint = 0x7CFC00;
		public static const lemonchiffon:uint = 16775885;
		public static const lightblue:uint = 11393254;
		public static const lightcoral:uint = 15761536;
		public static const lightcyan:uint = 14745599;
		public static const lightgoldenrodyellow:uint = 16448210;
		public static const lightgray:uint = 0xD3D3D3;
		public static const lightgreen:uint = 9498256;
		public static const lightgrey:uint = 0xD3D3D3;
		public static const lightpink:uint = 16758465;
		public static const lightsalmon:uint = 16752762;
		public static const lightseagreen:uint = 2142890;
		public static const lightskyblue:uint = 8900346;
		public static const lightslategray:uint = 7833753;
		public static const lightslategrey:uint = 7833753;
		public static const lightsteelblue:uint = 11584734;
		public static const lightyellow:uint = 16777184;
		public static const lime:uint = 0xFF00;
		public static const limegreen:uint = 3329330;
		public static const linen:uint = 16445670;
		public static const magenta:uint = 0xFF00FF;
		public static const maroon:uint = 0x800000;
		public static const mediumaquamarine:uint = 6737322;
		public static const mediumblue:uint = 205;
		public static const mediumorchid:uint = 12211667;
		public static const mediumpurple:uint = 9662683;
		public static const mediumseagreen:uint = 3978097;
		public static const mediumslateblue:uint = 8087790;
		public static const mediumspringgreen:uint = 64154;
		public static const mediumturquoise:uint = 4772300;
		public static const mediumvioletred:uint = 13047173;
		public static const midnightblue:uint = 1644912;
		public static const mintcream:uint = 16121850;
		public static const mistyrose:uint = 16770273;
		public static const moccasin:uint = 16770229;
		public static const navajowhite:uint = 16768685;
		public static const navy:uint = 128;
		public static const oldlace:uint = 16643558;
		public static const olive:uint = 0x808000;
		public static const olivedrab:uint = 7048739;
		public static const orange:uint = 0xFFA500;
		public static const orangered:uint = 0xFF4500;
		public static const orchid:uint = 14315734;
		public static const palegoldenrod:uint = 15657130;
		public static const palegreen:uint = 10025880;
		public static const paleturquoise:uint = 11529966;
		public static const palevioletred:uint = 14381203;
		public static const papayawhip:uint = 16773077;
		public static const peachpuff:uint = 16767673;
		public static const peru:uint = 13468991;
		public static const pink:uint = 16761035;
		public static const plum:uint = 14524637;
		public static const powderblue:uint = 11591910;
		public static const purple:uint = 0x800080;
		public static const rosybrown:uint = 12357519;
		public static const royalblue:uint = 4286945;
		public static const saddlebrown:uint = 9127187;
		public static const salmon:uint = 16416882;
		public static const sandybrown:uint = 16032864;
		public static const seagreen:uint = 3050327;
		public static const seashell:uint = 16774638;
		public static const sienna:uint = 10506797;
		public static const silver:uint = 0xC0C0C0;
		public static const skyblue:uint = 8900331;
		public static const slateblue:uint = 6970061;
		public static const slategray:uint = 7372944;
		public static const slategrey:uint = 7372944;
		public static const snow:uint = 16775930;
		public static const springgreen:uint = 65407;
		public static const steelblue:uint = 4620980;
		public static const tan:uint = 13808780;
		public static const teal:uint = 0x8080;
		public static const thistle:uint = 14204888;
		public static const tomato:uint = 16737095;
		public static const turquoise:uint = 4251856;
		public static const violet:uint = 15631086;
		public static const wheat:uint = 16113331;
		public static const white:uint = 0xFFFFFF;
		public static const whitesmoke:uint = 0xF5F5F5;
		public static const yellow:uint = 0xFFFF00;
		public static const yellowgreen:uint = 10145074;
		
		public function ColorHelper(c:pc)
		{
		}
		
		public static function RGBtoCMYK(...args):CMYKColor{
			var r:uint, g:uint, b:uint;
			if(args.length == 1 && args[0] is RGBColor){
				var rgb:RGBColor = args[0] as RGBColor;
				r = rgb.R;
				g = rgb.G;
				b = rgb.B;
			}else if(args.length > 2){
				r = parseInt(String(args[0]));
				g = parseInt(String(args[1]));
				b = parseInt(String(args[2]));
			}else{
				throw new ArgumentError('参数错误！(RGBColor)或者(r,g,b)');
			}
			var C3:Number = 1 - MathHelper.Range(r, 0, 255) / 255;
			var M3:Number = 1 - MathHelper.Range(g, 0, 255) / 255;
			var Y3:Number = 1 - MathHelper.Range(b, 0, 255) / 255;
			var K4:Number = FindMinimum(C3, M3, Y3);
			var cmyk:CMYKColor;
			
			if (K4 == 1) {
				cmyk = new CMYKColor(0, 0, 0, 1);
			}else {
				cmyk = new CMYKColor((C3 - K4) / (1 - K4),
					(M3 - K4) / (1 - K4),
					(Y3 - K4) / (1 - K4),
					K4);
			}
			return cmyk;
		}
		public static function CMYKtoRGB(...args):RGBColor
		{
			var c:uint, m:uint, y:uint, k:uint;
			if(args.length == 1 && args[0] is CMYKColor){
				var cmyk:CMYKColor = args[0] as CMYKColor;
				c = cmyk.C;
				m = cmyk.M;
				y = cmyk.Y;
				k = cmyk.K
			}else if(args.length > 3){
				c = parseInt(String(args[0]));
				m = parseInt(String(args[1]));
				y = parseInt(String(args[2]));
				k = parseInt(String(args[3]));
			}else{
				throw new ArgumentError('参数错误！(CMYKColor)或者(c,m,y,k)');
			}
			var cc:Number = MathHelper.Range(c, 0, 100) / 100;
			var cm:Number = MathHelper.Range(m, 0, 100) / 100;
			var cy:Number = MathHelper.Range(y, 0, 100) / 100;
			var ck:Number = MathHelper.Range(k, 0, 100) / 100;
			
			var C3:Number = cc * (1 - ck) + 1 * ck;
			if (C3>1) {C3=1;}
			
			var M3:Number = cm * (1 - ck) + 1 * ck;
			if (M3>1) {M3=1;}
			
			var Y3:Number = cy * (1 - ck)+ 1 * ck;
			if (Y3>1) {Y3=1;}
			
			var r:Number = 1 - C3;
			var g:Number = 1 - M3;
			var b:Number = 1 - Y3;
			
			return new RGBColor(NumToByte(r), NumToByte(g), NumToByte(b));
		}
		
		private static function NumToByte(n:Number):uint {
			return Math.round(n * 255);
		}
		private static function FindMinimum(C:Number ,M:Number ,Y:Number):Number {
			var min:Number = 1;
			if (C < min) { min = C }
			if (M < min) { min = M }
			if (Y < min) { min = Y }
			return min;
		}
	}//end class
}
class pc{}